package com.lwt.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lwt.yygh.hosp.repository.DepartmentRepository;
import com.lwt.yygh.hosp.service.DepartmentService;
import com.lwt.yygh.model.hosp.Department;
import com.lwt.yygh.vo.hosp.DepartmentQueryVo;
import com.lwt.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Weton Li
 * @Date 2021/4/23 15:31
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDept(Map<String, Object> paramMap) {
        // 将集合转为对象
        String jsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        Department isDepartmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (isDepartmentExist != null) {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            // 有对象数据，则更新
            departmentRepository.save(isDepartmentExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建pageable对象，设置当前页和每页记录数
        Pageable pageable = PageRequest.of(page-1, limit); // 0是第一页
        // 创建Example对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        // 将vo对象转化为真实对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department); // 对象将的属性快速赋值
        department.setIsDeleted(0);

        Example<Department> example = Example.of(department,exampleMatcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void removeDept(String hoscode, String depcode) {
        // 根据医院编号和科室编号查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    // 根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        // 创建List集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        // 根据医院标号，查询所有
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example); // 所有科室的全部信息
        // 在条件封装
        // 先得到大科室 进行bigcode分组
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            String bigCode = entry.getKey(); // 大科室编号
            List<Department> department1List = entry.getValue(); // 大科室编号对应的全部数据
            // 封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepcode(department1List.get(0).getBigname());
            // 封装小科室
            ArrayList<DepartmentVo> children = new ArrayList<>();
            for (Department department1 : department1List) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department1.getDepcode());
                departmentVo2.setDepname(department1.getDepname());
                // 封装到list集合
                children.add(departmentVo2);
            }
            // 把小科室的集合放在大科室的children中
            departmentVo1.setChildren(children);
            result.add(departmentVo1);
        }
        return result;
    }

    // 查询医院名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null) {
            return department.getDepname();
        }
        return null;
    }
}



